package com.du.limiting.sentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.du.limiting.sentinel.aop.DiySentinelResource;
import com.du.limiting.sentinel.model.Person;
import com.du.limiting.sentinel.model.User;
import com.du.limiting.sentinel.service.SentinelLimitingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author dujunguang
 * @date 2022/2/21 16:58
 * @description: TODO
 **/

@RestController
@Slf4j
public class SentinelLimitingController {

    @Resource
    private SentinelLimitingService sentinelLimitingService;


    @PostMapping("/sayHello")
    public String sayHello() {
        return "hello";
    }

    @PostMapping("/hotPoint")
    @DiySentinelResource("hotPoint")
    public User hotPoint(@RequestBody Person user) {
        log.info("hotPoint 热点资源限流");

//        Entry entry = null;
//        try {
//            entry = SphU.entry("hotPoint", EntryType.IN, 1, user.getName());
//        } catch (BlockException ex) {
//            log.error("资源被限流", ex);
//            throw new RuntimeException("资源被限流");
//        } finally {
//            if (entry != null) {
//                entry.exit(1, user.getName());
//            }
//        }

        return sentinelLimitingService.hotPoint(user.getName());
    }

    @PostMapping("/flowControlQps")
    @DiySentinelResource("flowControlQps")
    public User flowControlQps(@RequestBody Person user) {
        System.out.println("flowControlQps 流控Qps");
        return sentinelLimitingService.flowControlQps(user.getName());
    }

    @PostMapping("/flowControlThreadNum")
    public User flowControlThreadNum(@RequestBody User user) {
        System.out.println("flowControlThreadNum 流控线程数");
        return sentinelLimitingService.flowControlThreadNum(user.getName());
    }

    @PostMapping("/flowControlLimitApp")
    public User flowControlLimitApp(@RequestParam("name") String name,@RequestBody User user) {
        System.out.println("flowControlThreadNum 流控线程数");
        return sentinelLimitingService.flowControlLimitApp(user.getName());
    }



}
